<template>
    <div class="salesman">
        <search-form :searchArray="searchArray" :searchObj="searchObj"  @search="getList">
        </search-form>
        <elTable :formData="formData" @getList="getList">
                <el-table-column prop="name" label="业务员姓名" align="center"></el-table-column>
                <el-table-column prop="phone" label="业务员手机号" align="center">
                </el-table-column>
                <el-table-column prop="cardNo" label="业务员身份证号" align="center"></el-table-column>
                <el-table-column prop="statusMessage" label="业务员状态" align="center"></el-table-column>
                <el-table-column prop="loginTime" label="创建时间" align="center">
                    <template slot-scope="scope">
                        {{scope.row.createTime && $normalTime(scope.row.createTime) || ''}}
                    </template>
                </el-table-column>
                <el-table-column prop="action" label="操作" align="center" >
                    <template slot-scope="scope">
                        <el-button type="text" @click="clock(scope.row)">{{scope.row.status == -1 ? '解锁' : '锁定'}}</el-button>
                        <el-button type="text"  @click="goDetail(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
        </elTable>
    </div>
</template>
<script>
import elTable from "@/components/xForm/elTable"
import { mapGetters } from 'vuex'
export default {
    name: 'salesman',
    components: {
        elTable
    },
    data () {
        return {
            formData: {
                currentPage: 1,
                pageSize: 10,
                data: [
                ],
                total: 0
            },
            searchObj: {
                phone: '',
                name: '',
                cardNo: '',
                status: '',
                parentSalesManId: '',
                jobId: ''
            },
            form: {
                name: ''
            },
            visible: {
                show: true
            },
            searchArray: [
                {
                    type: "input",
                    placeholder: "请输入业务员姓名",
                    types: "text",
                    value: "",
                    label: "业务员姓名",
                    key: 'name'
                },
                {
                    type: "input",
                    placeholder: "请输入手机号",
                    types: "text",
                    value: "",
                    label: "手机号",
                    key: 'phone'
                },
                {
                    type: "input",
                    placeholder: "请输入身份证号码",
                    types: "text",
                    value: "",
                    label: "身份证号码",
                    key: 'cardNo'
                },
                {
                    type: "select",
                    placeholder: "请选择",
                    filterable: false,
                    value: "",
                    width: '',
                    label: '状态',
                    key: 'status',
                    list: [
                    ]
                },
                {
                    type: "select",
                    placeholder: "请选择",
                    filterable: false,
                    value: "",
                    width: '',
                    label: '职位',
                    key: 'jobId',
                    list: [
                    ]
                },
            ]
        }
    },
    computed: {
        ...mapGetters({
            enumAll: 'common/getEnumAll'
        }),
    },
    created () {
        this.init()
        this.getList()
    },
    methods: {
        init () {
            this.$set(this.searchArray[3], 'list', this.enumAll.EnumCompanyStatus || [])
            this.getJobList()
        },
        getList(status = true) {
            if (!status) this.formData.currentPage = 1
            const {formData: {currentPage, pageSize}, searchObj} = this
            let param = {}
            param = {
                currentPage,
                pageSize
            }
            for ( let k in searchObj) {
                if (searchObj[k] !== '') {
                    param[k] = searchObj[k]
                }
            }
            this.$loading(true)
            this.$api.userManage.getSalesManList(param).then(r => {
                this.formData.data = r.data && r.data.list || []
                this.formData.total = r.data.total || 0
            }).finally(() => {
                this.$loading(false)
            })
        },
        clock (item) {
            this.$confirm(`您确定要${item.status == -1 ? '解锁' : '锁定'}?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    let param = {}
                    param = {
                        id: item.id,
                        status: item.status == -1 ? 0 : -1
                    }
                    this.$api.userManage.putSalesmanChangeStataus(param).then(r => {
                        this.getList()
                    })
                }).catch(() => {
            })
        },
        goDetail (item) {
            this.$router.push({
                path: `/userManage/salesman/detail/${item.id}`
            })
        },
        // 获取 工作列表
        getJobList () {
            this.$api.common.getJobList().then(r => {
                let list = r.data || []
                list = list.map(r => {
                    return {
                        label: r.name,
                        value: r.name
                    }
                })
                this.$set(this.searchArray[4], 'list', list)
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.salesman {
    .search-form {
        margin-bottom: 15px;
    }
}
</style>